package com.itheima.mpermission.controller;

import com.itheima.mpermission.dto.PermissionAutoDTO;
import com.itheima.mpermission.dto.PermissionDTO;
import com.itheima.mpermission.dto.RepPermission;
import com.itheima.mpermission.dto.SubpermissionDTO;
import com.itheima.mpermission.service.PermissionService;
import com.leyou.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询全部权限
     * @return
     */
    @GetMapping("/findAll")
    public ResponseEntity<List<PermissionAutoDTO>> findAll(){
        List<PermissionAutoDTO> authList = permissionService.findAll();
        return ResponseEntity.ok(authList);
    }

    @GetMapping("/findByRoles")
    public ResponseEntity<List<PermissionAutoDTO>> findByRoles(HttpServletRequest request){
        List<PermissionAutoDTO> authList = permissionService.findByRoles(request);
        return ResponseEntity.ok(authList);
    }

    @GetMapping("/findSubPath")
    public ResponseEntity<List<SubpermissionDTO>> findSubPath(HttpServletRequest request){
        List<SubpermissionDTO> result=permissionService.findSubPath(request);
        return ResponseEntity.ok(result);
    }


    //用户页面展示
    @GetMapping("/list/page")
    public ResponseEntity<PageResult<PermissionDTO>> queryListByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5" )Integer rows
    ){
        PageResult<PermissionDTO> pageResult = permissionService.queryListByPage(page, rows);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/list/of/parent")
    public ResponseEntity<List<PermissionDTO>> findPermissionByParentId(@RequestParam("pid") Long pid){
        List<PermissionDTO> list = permissionService.findPermissionByParentId(pid);
        return ResponseEntity.status(HttpStatus.OK).body(list);
    }

    @GetMapping("/list/of/children")
    public ResponseEntity<List<PermissionDTO>> findPermissionById(@RequestParam("pid") Long pid){
        List<PermissionDTO> list = permissionService.findPermissionById(pid);
        return ResponseEntity.status(HttpStatus.OK).body(list);
    }

    @PostMapping
    public ResponseEntity<Void> addPermission(@RequestBody RepPermission repPermission){
        permissionService.addPermission(repPermission);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updatePermission(@RequestBody RepPermission repPermission){
        permissionService.updatePermission(repPermission);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> deletePermission(@PathVariable(value = "id") Long id){
        permissionService.deletePermission(id);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }


}
